package com.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public class MyDateUtil {

	public static final String sf_MD = "M/d";
	public static final String sfh_ymdhmss = "yyyy-MM-dd HH:mm:ss:SSS";
	public static final String sfh_ymdhms = "yyyy-MM-dd HH:mm:ss";
	public static final String sfh_ymdhm = "yyyy-MM-dd HH:mm";
	public static final String sfh_ymdh = "yyyy-MM-dd HH";
	public static final String sfh_ymd = "yyyy-MM-dd";
	public static final String sfh_ym = "yyyy-MM";
	public static final String sfh_y = "yyyy";

	public static final String sfz_y = "yyyy年";
	public static final String sfz_ym = "yyyy年MM月";
	public static final String sfz_ymd = "yyyy年MM月dd日";
	public static final String sfz_ymdh = "yyyy年MM月dd日 HH时";
	public static final String sfz_ymdhm = "yyyy年MM月dd日 HH时mm分";
	public static final String sfz_ymdhms = "yyyy年MM月dd日 HH时mm分ss秒";
	public static final String sfz_ymdhmss = "yyyy年MM月dd日 HH时mm分ss秒SSS";

	public static final String sf_ymdhmss = "yyyyMMddHHmmssSSS";
	public static final String sf_ymdhms = "yyyyMMddHHmmss";
	public static final String sf_ymdhm = "yyyyMMddHHmm";
	public static final String sf_ymdh = "yyyyMMddHH";
	public static final String sf_ymd = "yyyyMMdd";
	public static final String sf_ym = "yyyyMM";
	public static final String sf_y = "yyyy";



	/**
	 * 比较传入的日期和当前日期
	 * @param value
	 * @return
	 */
	public static boolean getCompareDate(String value) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(sfh_ymd);
			SimpleDateFormat dateFormat1 = new SimpleDateFormat(sf_ymd);
			Date parse1 = dateFormat.parse(value);
			String begin = dateFormat1.format(parse1);
			String end = dateFormat1.format(new Date());
			if (Integer.parseInt(begin) > Integer.parseInt(end)) {
				return true;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * day: 第几天前
	 * mark:
	 * 1.yyyy-MM-dd
	 * 2.yyyy-MM-dd HH:mm:ss
	 * 3.yyyy-MM-dd HH:mm:ss:SSS
	 *
	 * @return
	 */
	public static String getBeginDate(int day, String key) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
		return new SimpleDateFormat(key).format(calendar.getTime());
	}

	/***
	 * 获取当前时间
	 * @return
	 */
	public static String getStrCurrentDate(String key) {
		return new SimpleDateFormat(key).format(new Date());
	}

	/***
	 * 转换时间
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String str, String key) throws ParseException {
		return new SimpleDateFormat(key).parse(str);
	}

	/**
	 * 获取当前时间Date()
	 *
	 * @return
	 */
	public static Date getDate() {
		return new Date();
	}

	/**
	 * Date和String，二选一其中一个为null
	 * 参数三：
	 * @return
	 * @throws ParseException
	 */
	public static Long getDateCount(Date d, String s, String key) throws ParseException {
		if (d == null) {
			d = new SimpleDateFormat(key).parse(s);
		}
		return d.getTime();
	}

	/**
	 * 根据出生日期计算年龄
	 *
	 * @param brithday
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public static int getCurrentAgeByBirthdate(String brithday) throws Exception {
		try {
			Calendar calendar = Calendar.getInstance();
			SimpleDateFormat formatDate = new SimpleDateFormat(sfh_ymd);
			String currentTime = formatDate.format(calendar.getTime());
			Date today = formatDate.parse(currentTime);
			Date brithDay = formatDate.parse(brithday);
			return today.getYear() - brithDay.getYear();
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 计算两个日期相隔的天数
	 *
	 * @param firstString
	 * @param secondString
	 * @return
	 */
	public static int nDaysBetweenTwoDate(String firstString, String secondString) {
		Date firstDate = null;
		Date secondDate = null;
		try {
			secondDate = new SimpleDateFormat(sfh_ymd).parse(secondString);
			firstDate = new SimpleDateFormat(sfh_ymd).parse(firstString);
		} catch (Exception e) {
			// 日期型字符串格式错误
			System.out.println("日期型字符串格式错误");
		}
		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		return nDay;
	}

	/**
	 * 获取两个毫秒相差分钟
	 *
	 * @param time
	 * @return
	 */
	public static long getDateMinute(long time) {
		try {
			long currentTime = System.currentTimeMillis();
			//从对象中拿到时间
			long createTime = new SimpleDateFormat(sfh_ymdhms).parse(new SimpleDateFormat(sfh_ymdhms).format(time)).getTime();
			long diff = (currentTime - createTime) / 1000 / 60;
			return diff;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 两个日期比较
	 *
	 * @param begin 2022-10-01 11:22:22
	 * @param end   2022-11-01 11:22:22
	 * @return
	 */
	public static boolean compareTwoDate(String begin, String end) {
		try {
			Date beginDate = new SimpleDateFormat(sfh_ymdhms).parse(begin);
			Date endDate = new SimpleDateFormat(sfh_ymdhms).parse(end);
			return beginDate.before(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
	}


}
